package cn.tedu.egweibo2.controller;

import cn.tedu.egweibo2.mapper.UserMapper;
import cn.tedu.egweibo2.pojo.dto.UserRegDTO;
import cn.tedu.egweibo2.pojo.entity.User;
import cn.tedu.egweibo2.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Api(tags = "用户模块")
@Slf4j
@RestController
@RequestMapping("/v1/users/")
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @ApiOperation(value = "用户注册功能")
    @PostMapping("reg")
    public int reg(@RequestBody UserRegDTO userRegDTO){
        /**
         * 1.判断占用名select
         * 2.插入数据insert
         */

        log.debug("userRegDTO = " + userRegDTO);

        String username = userRegDTO.getUsername();
        UserVO userVO = userMapper.selectByUsername(username);


        log.debug("userVO = " + userVO);
        if(userVO!=null){
            return 2;
        }

        User user = new User();
        BeanUtils.copyProperties(userRegDTO,user);
        user.setCreated(new Date());
        log.debug("user = " + user);

        userMapper.insertUser(user);
        return 1;
    }
}
